// mypage/accBill/accBill.js
var app=getApp();
var other = require("../../utils/other");
var util=require("../../utils/util");
var my=require("../../api/my");

Page({
  /*页面的初始数据*/
  data: {
    title: '账单',
    navColor: 'rgba(255,255,255,0)',
    showBack_to: true,
    showAgain:false,
    year: '',
    month: '',
    money1: '',
    money2: '',
    nav_bar: ["支出","收入"],
    nav_cur: 0,
    bill_list: [],
    pop_show: true,
    com_data: [],
    value: [0,0],
    currentPage: 1,
    pageSize: 10,
    hasMoreData: true,
    token: ''
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that = this,now=new Date();
    var year = now.getFullYear(),month = (now.getMonth() + 1)<10? "0"+(now.getMonth() + 1):(now.getMonth() + 1);
    var money1=2231.85,money2=15444.45;
    var monthStart = new Date(year, now.getMonth(), 1); //本月的开始时间
    var timeStar=monthStart.getTime(),timeEnd=now.getTime();
    that.setData({year:year,month:month,money1:util.toThousands(util.toDecimal(money1)),money2:util.toThousands(util.toDecimal(money2)),timeStar:timeStar,timeEnd:timeEnd,token:wx.getStorageSync('token')})
    // 
    that.get_bill_list(0,timeStar,timeEnd)
  },
  // 切换
  change_nav:function(e){
    var that = this,nav_cur=that.data.nav_cur,timeStar=that.data.timeStar,timeEnd=that.data.timeEnd;
    var index=e.currentTarget.dataset.index;
    if(nav_cur != index){
      that.setData({nav_cur: index,currentPage:1,bill_list:[]});
      //
      that.get_bill_list(index,timeStar,timeEnd)
    }
  },
  //选择时间
  check_time:function(){
    var that=this;
    var obj = other.dateTimePicker(2018,new Date().getFullYear())
    that.setData({com_data: obj.dateTimeArray,pop_show: !that.data.pop_show})
  },
  close_pop:function(){
    this.setData({pop_show: !this.data.pop_show})
  },
  picker_change_time:function(e){
    var that = this;
    var val = e.detail.value;
    that.setData({value: val})
  },
  confirm_time:function(){
    var that=this,nav_cur=that.data.nav_cur,com_data=that.data.com_data,value=that.data.value;
    var year=com_data[0][value[0]],month=Number(com_data[1][value[1]].replace(/^0/, ''));
    var timeStar = new Date(year, month-1, 1).getTime(); //本月的开始时间
    var timeEnd = new Date(year, month, 0).getTime(); //本月的结束时间
    that.setData({bill_list:[],currentPage:1,timeStar:timeStar,timeEnd:timeEnd})
    that.close_pop();
    //
    that.get_bill_list(timeStar,timeEnd)
  },
  // 触底
  scroll_bottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,nav_cur=that.data.nav_cur,timeStar=that.data.timeStar,timeEnd=that.data.timeEnd;
    if(hasMoreData){
      that.get_bill_list(nav_cur,timeStar,timeEnd)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 获取账单列表
  get_bill_list:function(type,timeStar,timeEnd){
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token;
    wx.showLoading({title: '加载中'})
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "type": type,
      "startTime": timeStar,
      "endTime": timeEnd
    }
    my.get_bill_list(params,token).then(res=>{
      wx.hideLoading()
      if(res.data.code==200){
        var list=res.data.data.records;
        if(list.length>0){
          for(var i=0;i<list.length;i++){
            list[i].year=util.formatTime_age(new Date(list[i].createTime)).split("-")[0]
            list[i].month=util.formatTime_age(new Date(list[i].createTime)).split("-")[1]
            list[i].day=util.formatTime_age(new Date(list[i].createTime)).split("-")[2]
            list[i].time=util.formatTime_month_else(new Date(list[i].createTime))
            list[i].hour=util.formatTime_time(new Date(list[i].createTime))
          }
          list=that.deal_with_data(list)
        }
        var bill_list = that.data.bill_list;
        bill_list = util.extend(bill_list, list)
        if (list.length < pageSize) {
          that.setData({
            bill_list: bill_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            bill_list: bill_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
      }
    })
  },
  // 处理数据
  deal_with_data:function(list){
    var that=this,timeStar=that.data.timeStar,bill_arr=[];
    var year=util.formatTime_age(new Date(timeStar)).split("-")[0],month=util.formatTime_age(new Date(timeStar)).split("-")[1];
    var month_arr = other.getMonthDay(year, month);
    for(var i=0;i<month_arr.length;i++){
      var data={time:'',day:month_arr[i],arr:[]}
      bill_arr.push(data)
    }
    for(var m=0;m<bill_arr.length;m++){
      for(var n=0;n<list.length;n++){
        if(bill_arr[m].day==list[n].day){
          bill_arr[m].time=list[n].time
          bill_arr[m].arr.push(list[n])
        }
      }
    }
    return bill_arr
  },
})